package com.atguigu.eduservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CourseQueryVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.handler.CustomException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2023-03-16
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
	@Resource
	private EduCourseDescriptionService eduCourseDescription;

	@Resource
	private EduCourseMapper eduCourseMapper;

	@Override
	public String saveCourseInfo(CourseInfoVo courseInfoForm) {

		EduCourse eduCourse = BeanUtil.copyProperties(courseInfoForm, EduCourse.class);

		this.save(eduCourse);

		//保存课程详情信息
		EduCourseDescription courseDescription = new EduCourseDescription();
		courseDescription.setDescription(courseInfoForm.getDescription());
		courseDescription.setId(eduCourse.getId());
		boolean save = eduCourseDescription.save(courseDescription);
		if(!save){
			throw new CustomException(20001, "课程详情信息保存失败");
		}

		return eduCourse.getId();

	}

	@Override
	public Map<String, Object> pageListWeb(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
		QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
			queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
		}

		if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
			queryWrapper.eq("subject_id", courseQuery.getSubjectId());
		}

		if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
			queryWrapper.orderByDesc("buy_count");
		}

		if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
			queryWrapper.orderByDesc("gmt_create");
		}

		if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
			queryWrapper.orderByDesc("price");
		}

		baseMapper.selectPage(pageParam, queryWrapper);

		List<EduCourse> records = pageParam.getRecords();
		long current = pageParam.getCurrent();
		long pages = pageParam.getPages();
		long size = pageParam.getSize();
		long total = pageParam.getTotal();
		boolean hasNext = pageParam.hasNext();
		boolean hasPrevious = pageParam.hasPrevious();

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("items", records);
		map.put("current", current);
		map.put("pages", pages);
		map.put("size", size);
		map.put("total", total);
		map.put("hasNext", hasNext);
		map.put("hasPrevious", hasPrevious);

		return map;

	}

	@Override
	public List<CourseWebVo> getBaseCourseInfo(String courseId) {
		return eduCourseMapper.getBaseCourseInfo(courseId);
	}

}
